﻿// -----------------------------------------------------------------------
// <copyright file="Particle.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace SpaceshipDuel
{
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    public enum ParticleType
    {
        Ship1RocketEngine = 0,
        Ship2RocketEngine = 1,
        AlienEngine = 2
    }

    /// <summary>
    /// Particle class
    /// </summary>
    public class Particle : GameObject
    {
        #region Private fields

        WaitTimer lifeTimer;    // Timer für Lebenszyklus des Partikels (fade-out)
        bool destroyMe;         // Selbstzerstörungsflag des Objekts
        Color myColor;          // Partikelfarbe (abhängig vom Alter)
        double maxAge;
        Vector2 direction;
        ParticleType type;

        #endregion Private fields

        #region Public properties

        public bool DestroyMe
        {
            get { return this.destroyMe; }
            set { this.destroyMe = value; }
        }

        public ParticleType Type
        {
            get { return type; }
        }

        #endregion Public properties

        #region Construction

        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="newTexture">Textur</param>
        /// <param name="newPosition">Position</param>
        /// <param name="newType">Explosionstyp</param>
        public Particle(TextureData newTextureData, Vector2 newCenter, double lifeTime, Vector2 newDirection, ParticleType newType)
        {
            textureData = newTextureData;
            center = newCenter;
            lifeTimer = new WaitTimer(lifeTime);
            maxAge = lifeTime;
            destroyMe = false;
            myColor = new Color(255, 255, 255);
            direction = newDirection;
            type = newType;
        }

        #endregion Construction

        #region Public methods

        /// <summary>
        /// Update-Methode für den Schuss
        /// </summary>
        public void Update(GameTime gameTime)
        {
            if (destroyMe == false)
            {
                center += direction;
                CheckPosition();

                if (lifeTimer.IsElapsed(gameTime.ElapsedGameTime.TotalSeconds))
                {
                    destroyMe = true;
                }
                else
                {
                    int age = 255 - (int)(lifeTimer.GetTimeSinceLastCheck() / maxAge * 255f);

                    if (type == ParticleType.Ship1RocketEngine)
                    {
                        if (age > 200)
                            myColor = new Color(age, age, age/3, age/2);
                        else if (age > 150)
                            myColor = new Color(age, age / 2, 0, age/2);
                        else if (age > 100)
                            myColor = new Color(age, age / 4, 0, age/2);
                        else if (age > 50)
                            myColor = new Color(age, 0, 0, age/2);
                    }
                    else if (type == ParticleType.Ship2RocketEngine)
                    {
                        if (age > 250)
                            myColor = new Color(age, age, age / 2, age / 2);
                        else if (age > 220)
                            myColor = new Color(age / 6, age / 2, age, age / 2);
                        else if (age > 170)
                            myColor = new Color(age / 8, age / 3, age, age / 2);
                        else if (age > 100)
                            myColor = new Color(0, age / 4, age, age / 2);
                        else if (age > 50)
                            myColor = new Color(0, 0, age, age / 2);
                    }
                    else
                    {
                        if (age > 230)
                            myColor = new Color(age / 2, age, age / 2, age / 2);
                        else if (age > 170)
                            myColor = new Color(age / 3, age, age / 3, age / 2);
                        else if (age > 120)
                            myColor = new Color(age / 4, age, age / 4, age / 2);
                        else if (age > 70)
                            myColor = new Color(age / 8, age, age / 8, age / 2);
                        else if (age > 50)
                            myColor = new Color(0, age, 0, age / 2);
                    }
                }
            }
        }

        /// <summary>
        /// Draw-Methode
        /// </summary>
        /// <param name="spriteBatch">Spritebatch</param>
        /// <param name="xOffset">Offset X</param>
        /// <param name="yOffset">Offset Y</param>
        new public void Draw(SpriteBatch spriteBatch, float xOffset, float yOffset)
        {
            if (destroyMe == false)
            {
                Rectangle destRect = new Rectangle((int)(center.X - textureData.halfwidth + xOffset), (int)(center.Y - textureData.halfheight + yOffset), textureData.tileWidth, textureData.tileHeight);
                spriteBatch.Draw(textureData.texture, destRect, myColor);
            }
        }

        #endregion Public methods

        #region Private methods

        /// <summary>
        /// Korrektur der Raumschiffposition beim Flug über Spielfeldgrenzen hinweg
        /// </summary>
        private void CheckPosition()
        {
            if (center.X < 0)
                center.X = worldSize + center.X;
            else if (center.X >= worldSize)
                center.X = center.X - worldSize;

            if (center.Y < 0)
                center.Y = worldSize + center.Y;
            else if (center.Y >= worldSize)
                center.Y = center.Y - worldSize;
        }

        #endregion private methods
    }
}
